<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/4/2
 * Time: 9:20
 */
namespace app\admin\controller;


use think\Db;
use think\Request;
class Luck extends Base {
    public function index(){
        if(request()->isAjax()){
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('lucky')->count();// 获取总条数
            $lists = Db::name('lucky')
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $name =input('name');
            $gold= input('gold');
            $odds= input('odds');
            $bai= input('bai');
            $data['name'] = $name;
            $data['gold'] = $gold;
            $data['odds'] =$odds;
            $data['bai'] =$bai;
            $data['add_time'] =date('Y-m-d H:i:s',time());
            $res = Db::table('up_lucky')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $name =input('name');
            $gold= input('gold');
            $odds= input('odds');
            $bai= input('bai');
            $data['name'] = $name;
            $data['gold'] = $gold;
            $data['odds'] =$odds;
            $data['bai'] =$bai;

            $res = Db::table('up_lucky')->where('id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('id');
        $info= Db::table('up_lucky')->where('id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$info);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('lucky')->where('id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}